using System;
using System.IO;
using System.Management.Automation;
using Nivot.PowerShell.ObjectModel;
using Nivot.PowerShell.WindowsMobile.Rapi2;

namespace Nivot.PowerShell.WindowsMobile
{
    internal class WindowsMobileObjectModel : IndexerObjectModelBase<WindowsMobileProvider>
    {
        private readonly RemoteDevice _device;

        internal WindowsMobileObjectModel(RemoteDevice device, string root)
            : base(root)
        {
            _device = device;            
        }

        internal bool IsConnected { get; set; }

        internal RemoteDevice Device
        {
            get { return _device; }
        }

        private void EnsureConnected()
        {
            if (!IsConnected)
            {
                Provider.ThrowTerminatingError(
                    new ErrorRecord(new InvalidOperationException("Device not connected."), "NoConnection",
                                    ErrorCategory.DeviceError, null));
            }
        }

        protected override IStoreItem GetRootStoreItem()
        {
            EnsureConnected();

            IStoreItem storeItem = new WindowsMobileFolder(new CEFolder(_device, this.Root));
            return storeItem;
        }

        public override bool ItemExists(string path)
        {
            bool exists = false;

            TryRapiOperation(delegate
                 {
                     CEFileAttributes attributes = Device.GetFileAttributes(path);
                     if ((attributes & CEFileAttributes.Hidden) == CEFileAttributes.Hidden)
                     {
                         // only return true for hidden files if Force is present
                         if (Provider.Force.IsPresent)
                         {
                             exists = true;
                         }
                     }
                     else
                     {
                         // normal file
                         exists = true;
                     }
                 }, false);

            return exists;
        }

        public override IStoreItem GetItem(string path)
        {
            IStoreItem item = null;

            TryRapiOperation(delegate
                 {
                     CEFileAttributes attributes = Device.GetFileAttributes(path);

                     if ((attributes & CEFileAttributes.Directory) == CEFileAttributes.Directory)
                     {
                         item = new WindowsMobileFolder(new CEFolder(_device, path));
                     }
                     else
                     {
                         item =
                             new WindowsMobileFile(
                                 (CEFile) CEObjectFactory.CreateCEObject(_device, path));
                     }            
                 }, true);
            return item;
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {                    
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }


        internal static void TryRapiOperation(Action action, bool terminateIfError)
        {
            try
            {
                action.Invoke();
            }
            catch (RapiException ex)
            {
                var error = new ErrorRecord(ex, "RapiError", ErrorCategory.DeviceError, null);
                
                if (terminateIfError)
                {
                    Provider.ThrowTerminatingError(error);
                }
                else
                {
                    Provider.WriteError(error);
                }
            }
        }
    }

    internal delegate void Action();
}